clear*
set maxvar 11000

global date _2025_10_28
cd "/Users/atri0032/Dropbox/World Bank/Temperature, poverty, and inequality/Estimations/"

******************************************************************************************
********************************** Table 1: The effects of temperature on poverty and inequality by income group
******************************************************************************************

* FE model 
use "${date}/_data/spid_for_analysis_v2.dta", clear

	sort code year
	merge m:1 code year using "${date}/_data/income_classification.dta"
	drop if _merge==2
	drop _merge

* Income classification based on 1st data point
bysort code (year): gen income_classification_1st = income_classification[1]
	
estimates clear	
	
local controls ln_GDPppp Unempilo agri manuf popgr 
	
reghdfe poor215 temp rainfall_sum humid `controls' if inlist(income_classification_1st,"L","LM"), absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	est store m1
reghdfe poor215 temp rainfall_sum humid `controls' if inlist(income_classification_1st,"H","UM"), absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	est store m2
reghdfe gini temp rainfall_sum humid `controls' if inlist(income_classification_1st,"L","LM"), absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	est store m3
reghdfe gini temp rainfall_sum humid `controls' if inlist(income_classification_1st,"H","UM"), absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	est store m4
	
local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2, fmt(0 3) ///
	label("Observations" "R-squared")) ///	
	eqlabels(none) mtitle("Poverty - low inc" "Poverty - high inc" "Gini - low inc" "Gini - high inc") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("Table 1 - FE")
	
esttab m*, `opts' 
esttab m* using "${date}/_tables/_tab_1_FE.csv", replace `opts' 

* LD model 
bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
encode geo_code2_new, gen(id_region)	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf edupri eduter popgr tech {
	gen `v'_d = D.`v'
}
	
collapse (mean) poor215_d poor365_d poor685_d gini_d theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d, by(geo_code2_new year income_classification_1st)
	
estimates clear	

local controls ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d   

reg poor215_d temp_d rainfall_sum_d humid_d `controls' if inlist(income_classification_1st,"L","LM"), cluster(geo_code2_new) 
	est store m1	
reg poor215_d temp_d rainfall_sum_d humid_d `controls' if inlist(income_classification_1st,"H","UM"), cluster(geo_code2_new) 	
	est store m2
reg gini_d temp_d rainfall_sum_d humid_d `controls' if inlist(income_classification_1st,"L","LM"), cluster(geo_code2_new) 
	est store m3	
reg gini_d temp_d rainfall_sum_d humid_d `controls' if inlist(income_classification_1st,"H","UM"), cluster(geo_code2_new) 
	est store m4	
	
local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2, fmt(0 3) ///
	label("Observations" "R-squared")) ///	
	eqlabels(none) mtitle("Poverty - low inc" "Poverty - high inc" "Gini - low inc" "Gini - high inc") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("Table 1 - LD")	
	
esttab m*, `opts' 	
esttab m* using "${date}/_tables/_tab_1_LD.csv", replace `opts' 

//==========================//	
	
// T-test: Lower income vs Higher income
* Fixed effects model
use "${date}/_data/spid_for_analysis_v2.dta", clear

encode geo_code2_new, gen(id_region)

	sort code year
	merge m:1 code year using "${date}/_data/income_classification.dta"
	drop if _merge==2
	drop _merge

* Income classification based on 1st data point
bysort code (year): gen income_classification_1st = income_classification[1]
	
qui reg poor215 temp rainfall_sum humid b2010.year b42.id_region ln_GDPppp Unempilo agri manuf popgr if inlist(income_classification_1st,"L","LM")	
	est store low_inc
qui reg poor215 temp rainfall_sum humid b2010.year b42.id_region ln_GDPppp Unempilo agri manuf popgr if inlist(income_classification_1st,"H","UM")
	est store high_inc
	
qui suest low_inc high_inc	
test [low_inc_mean]temp - [high_inc_mean]temp = 0

estimates clear
qui reg gini temp rainfall_sum humid b2010.year b42.id_region ln_GDPppp Unempilo agri manuf popgr if inlist(income_classification_1st,"L","LM")
	est store low_inc
qui reg gini temp rainfall_sum humid b2010.year b42.id_region ln_GDPppp Unempilo agri manuf popgr if inlist(income_classification_1st,"H","UM")
	est store high_inc
	
qui suest low_inc high_inc	
test [low_inc_mean]temp - [high_inc_mean]temp = 0

* LD model
use "${date}/_data/spid_for_analysis_v2.dta", clear

	sort code year
	merge m:1 code year using "${date}/_data/income_classification.dta"
	drop if _merge==2
	drop _merge

* Income classification based on 1st data point
bysort code (year): gen income_classification_1st = income_classification[1]
	
bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
encode geo_code2_new, gen(id_region)	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf edupri eduter popgr tech {
	gen `v'_d = D.`v'
}
	
estimates clear	
qui reg poor215_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d  if inlist(income_classification_1st,"L","LM")
	est store low_inc
qui reg poor215_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d  if inlist(income_classification_1st,"H","UM")
	est store high_inc	
	
qui suest low_inc high_inc	
test [low_inc_mean]temp_d - [high_inc_mean]temp_d = 0

estimates clear	
qui reg gini_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d  if inlist(income_classification_1st,"L","LM")
	est store low_inc
qui reg gini_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d  if inlist(income_classification_1st,"H","UM")
	est store high_inc	
	
qui suest low_inc high_inc	
test [low_inc_mean]temp_d - [high_inc_mean]temp_d = 0
	
// T-test: FE vs LD
use "${date}/_data/spid_for_analysis_v2.dta", clear

encode geo_code2_new, gen(id_region)

	sort code year
	merge m:1 code year using "${date}/_data/income_classification.dta"
	drop if _merge==2
	drop _merge

* Income classification based on 1st data point
bysort code (year): gen income_classification_1st = income_classification[1]
	
qui reg poor215 temp rainfall_sum humid i.id_region i.year ln_GDPppp Unempilo agri manuf popgr if inlist(income_classification_1st,"L","LM")
	est store fe_poor_lowinc
qui reg gini temp rainfall_sum humid i.id_region i.year ln_GDPppp Unempilo agri manuf popgr if inlist(income_classification_1st,"L","LM")
	est store fe_gini_lowinc
	
qui reg poor215 temp rainfall_sum humid i.id_region i.year ln_GDPppp Unempilo agri manuf popgr if inlist(income_classification_1st,"H","UM")
	est store fe_poor_highinc
qui reg gini temp rainfall_sum humid i.id_region i.year ln_GDPppp Unempilo agri manuf popgr if inlist(income_classification_1st,"H","UM")
	est store fe_gini_highinc
	
bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf edupri eduter popgr tech {
	gen `v'_d = D.`v'
}
	
qui reg poor215_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d  if inlist(income_classification_1st,"L","LM")
	est store ld_poor_lowinc	
qui reg gini_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d  if inlist(income_classification_1st,"L","LM")
	est store ld_gini_lowinc	
	
qui reg poor215_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d  if inlist(income_classification_1st,"H","UM")
	est store ld_poor_highinc	
qui reg gini_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d  if inlist(income_classification_1st,"H","UM")
	est store ld_gini_highinc	
	
qui suest fe_poor_lowinc ld_poor_lowinc	
test [fe_poor_lowinc_mean]temp - [ld_poor_lowinc_mean]temp_d = 0

qui suest fe_gini_lowinc ld_gini_lowinc
test [fe_gini_lowinc_mean]temp - [ld_gini_lowinc_mean]temp_d = 0

qui suest fe_poor_highinc ld_poor_highinc	
test [fe_poor_highinc_mean]temp - [ld_poor_highinc_mean]temp_d = 0

qui suest fe_gini_highinc ld_gini_highinc
test [fe_gini_highinc_mean]temp - [ld_gini_highinc_mean]temp_d = 0

